package org.example.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.Objects;

/**
 * @Author lyz
 * @Date 2025/3/12 14:34
 */
public class CookieUtils {

    /**
     * @param data documnet.cookie=<script></script>
     * @return
     */
    public static String go(JSONObject data) {
        String[] bts = data.getObject("bts", String[].class);
        String ct = data.getString("ct");
        String[] chars = data.getString("chars").split("");
        String ha = data.getString("ha");
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars.length; j++) {
                String i1 = chars[i];
                String j1 = chars[j];
                String cookie = bts[0] + i1 + j1 + bts[1];
                if (Objects.equals(hash(cookie, ha), ct)) {
                    return "__jsl_clearance_s=" + cookie;
                }
            }
        }
        return "";

    }

    /**
     * 解密cookie
     * @param cookie
     * @param ha
     * @return
     */
    private static String hash(String cookie, String ha){
        String str = "";
        try {
            switch (ha){
                case "sha1":str = HeaUtil.sha1(cookie);break;
                case "sha256":str = HeaUtil.sha256(cookie);break;
                case "md5":str = HeaUtil.md5(cookie);break;
                default:break;
            }
            return str;
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;
    }
}
